package io.dbw.common.utils;


import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;

public class NumberUtil
{
    public static double round(double value, int position)
    {
        BigDecimal bd = new BigDecimal(value);
        bd = bd.movePointLeft(position);
        bd = new BigDecimal(Math.round(bd.doubleValue()));
        bd = bd.movePointRight(position);
        double retvalue = bd.doubleValue();
        bd = null;
        return retvalue;
    }

    public static Boolean isNullOrZero(Long money){
        if(null==money||0==money){
            return true;
        }
        return false;
    }
    private final static BigDecimal b=new BigDecimal(0);

    public static Boolean isNullOrZero(BigDecimal money){
        if(null==money|| money.compareTo(b)==0){
            return true;
        }
        return false;
    }

    public static Boolean isNullOrZero(Float money){
        if(null==money||0==money){
            return true;
        }
        return false;
    }

    public static Boolean isNullOrZero(Integer money){
        if(null==money||0==money){
            return true;
        }
        return false;
    }

    public static String getCurrency(String str)
    {
        return getCurrency(str, false);
    }

    public static String getCurrency(String str, boolean $)
    {
        NumberFormat n = NumberFormat.getCurrencyInstance();

        String outStr = null;
        try {
            double d = Double.parseDouble(str);
            outStr = n.format(d);
            if (!$){
                outStr = outStr.substring(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outStr;
    }

    public static String getDecimalFormat(String str, int num)
    {
        String s = "######0";
        for (int i = 0; i < num; i++) {
            if (i == 0){
                s = s + ".0";
            } else {
                s = s + "0";
            }
        }
        DecimalFormat df = new DecimalFormat(s);
        return df.format(Double.parseDouble(str));
    }

    public static String getDecimalFormat(double str, int num)
    {
        String s = "######0";
        for (int i = 0; i < num; i++) {
            if (i == 0){
                s = s + ".0";
            } else {
                s = s + "0";
            }
        }
        DecimalFormat df = new DecimalFormat(s);
        return df.format(str);
    }

    public static String getCurrency(double d)
    {
        return getCurrency(d, false);
    }

    public static String getCurrency(double d, boolean $)
    {
        NumberFormat n = NumberFormat.getCurrencyInstance();
        String outStr = null;
        try {
            outStr = n.format(d);
            if (!$){
                outStr = outStr.substring(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outStr;
    }

    public static void main(String[] args) {
        BigDecimal dd=new BigDecimal(123456789.67);
        String s="123456789.67";
        Integer ii=1234567890;
        System.out.println(dd);
        Long orderMoney=123456789012L;
        double doubleValue=orderMoney.doubleValue()/100;
        System.out.println(doubleValue);
        String format1 = String.format("%.2f", doubleValue);
        System.out.println(format1);
        System.out.println(convertToChineseNum( format1));

    }

    public static String convertToChineseNum(String sInputString)
    {
        String sOutString = "";
        String sNumList = "零壹贰叁肆伍陆柒捌玖";
        String sRmbList = "分角元拾佰仟万拾佰仟亿拾佰仟万";
        float dNumber = 0.0F;
        String sTempsOutString = "";
        sInputString = StringUtils.replace(sInputString, ",", "");
        try
        {
            dNumber = Float.parseFloat(sInputString);
        }
        catch (Exception e)
        {
            return "传入参数非数字！";
        }

        if (dNumber > 9999999999999.9902D)
        {
            sOutString = "超出范围的人民币值";
            return sOutString;
        }

        if (sInputString.indexOf(".") != -1)
        {
            String a = sInputString.substring(0, sInputString.indexOf("."));
            String b = sInputString.substring(sInputString.indexOf(".") + 1);
            if (b.length() == 1){
                a = a + b + "0";
            } else{
                a = a + b.substring(0, 2);
            }

            sInputString = a;
        }
        else {
            sInputString = sInputString + "00";
        }String tempdNumberString = sInputString;
        int tempNmberLength = tempdNumberString.length();
        int i = 0;
        while (i < tempNmberLength)
        {
            int onedNumber = Integer.parseInt(tempdNumberString.substring(i, i + 1));
            String onedNumberChar = sNumList.substring(onedNumber, onedNumber + 1);

            String onedNumberUnit = sRmbList.substring(tempNmberLength - i - 1, tempNmberLength - i);
            if (onedNumberChar != "零") {
                sTempsOutString = sTempsOutString + onedNumberChar + onedNumberUnit;
            }
            else {
                if ((onedNumberUnit != "亿") && (onedNumberUnit != "万") && (onedNumberUnit != "元")) { if (onedNumberUnit != "零");
                } else {
                    while (sTempsOutString.endsWith("零"))
                    {
                        sTempsOutString = sTempsOutString.substring(0, sTempsOutString.length() - 1);
                    }
                }

                if ((onedNumberUnit == "亿") || ((onedNumberUnit == "万") && (!sTempsOutString.endsWith("亿"))) || (onedNumberUnit == "元"))
                {
                    sTempsOutString = sTempsOutString + onedNumberUnit;
                }
                else
                {
                    boolean tempEnd = sTempsOutString.endsWith("亿");
                    boolean zeroEnd = sTempsOutString.endsWith("零");
                    if (sTempsOutString.length() > 1)
                    {
                        boolean zeroStart = sTempsOutString.substring(sTempsOutString.length() - 2, sTempsOutString.length()).startsWith("零");
                        if ((!zeroEnd) && ((zeroStart) || (!tempEnd))) {
                            sTempsOutString = sTempsOutString + onedNumberChar;
                        }

                    }
                    else if ((!zeroEnd) && (!tempEnd)) {
                        sTempsOutString = sTempsOutString + onedNumberChar;
                    }
                }
            }
            i++;
        }

        while (sTempsOutString.endsWith("零"))
        {
            sTempsOutString = sTempsOutString.substring(0, sTempsOutString.length() - 1);
        }

        while (sTempsOutString.endsWith("元"))
        {
            sTempsOutString = sTempsOutString + "整";
        }
        sOutString = sTempsOutString;
        return sOutString;
    }

    public static int getGBD(int iNumA, int iNumB)
    {
        int t = 0; int aa = 0; int bb = 0;
        if (iNumA < iNumB)
        {
            t = iNumA;
            iNumA = iNumB;
            iNumB = t;
        }
        aa = iNumA;
        bb = iNumB;
        int temp = 0;
        do
        {
            temp = aa % bb;
            aa = bb;
            bb = temp;
        }
        while (bb != 0);

        return aa;
    }

    public static int getRandomNum(int a, int b)
    {
        int temp = 0;
        try
        {
            if (a > b)
            {
                temp = new Random().nextInt(a - b);
                return temp + b;
            }

            temp = new Random().nextInt(b - a);
            return temp + a;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return temp + a;
    }

    public static String getStorageSize(double dSize)
    {
        if (dSize == 0.0D)
        {
            return "0B";
        }

        if (dSize < 1024.0D)
        {
            return dSize + "B";
        }

        if (dSize < 1048576.0D)
        {
            return round(dSize / 1000.0D, -1) + "K";
        }

        if (dSize < 1073741824.0D)
        {
            return round(dSize / 1000000.0D, -1) + "M";
        }

        return round(dSize / 1000000000.0D, -1) + "G";
    }

    public static String getNumWithoutEndZero(double dNum)
    {
        String sNum = String.valueOf(dNum);
        if (sNum.indexOf(".") != -1)
        {
            while (sNum.lastIndexOf("0") == sNum.length() - 1)
            {
                sNum = sNum.substring(0, sNum.length() - 1);
            }
            if (sNum.indexOf(".") == sNum.length() - 1)
            {
                sNum = sNum.substring(0, sNum.length() - 1);
            }
        }
        return sNum;
    }

    public static Integer toInteger(String sVal,String sDef){
        if(StringUtils.isBlank(sVal)){
            return Integer.parseInt(sDef);
        }
        return Integer.parseInt(sVal);
    }
    public static Integer toInteger(String sVal){
        return toInteger(sVal,"0");
    }

}